<template>
  <div class="knowledge-article-management">
    <!-- 搜索及筛选区域 -->
    <div class="search-section">
      <el-form :inline="true" :model="searchForm" ref="searchForm">
        <!-- <el-form-item label="文章类型">
          <el-select
            v-model="searchForm.articleType"
            placeholder="全部"
            clearable
          >
            <el-option
              v-for="item in articleTypeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="文章搜索">
          <el-input
            v-model="searchForm.searchKeywords"
            placeholder="输入标题"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 数据表格 -->
    <el-table
      :data="tableData"
      stripe
      v-loading="tableLoading"
      element-loading-text="正在加载数据..."
      :border="false"
    >
      <el-table-column
        label="序号"
        width="80"
        align="center"
      >
        <template #default="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
  prop="title"
  label="标题"
  min-width="300"
  align="center"
>
  <template #default="scope">
    <span class="clickable-title" @click="handleTitleClick(scope.row)">
      {{ scope.row.title }}
    </span>
  </template>
</el-table-column>
      <el-table-column
        label="操作"
        width="100"
        align="center"
      >
        <template #default="scope">
          <el-button
            type="text"
            icon="star-on"
            @click="handleCollect(scope.row)"
          >
            收藏
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10,20,30]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: "KnowledgeArticleManagement",
  data() {
    return {
      searchForm: {
        searchKeywords: "",
      },
      
      tableData: [      ],
      tableLoading: false,
      totalCount: 5, // 示例数据总数
      currentPage: 1,
      pageSize: 10,
    };
  },
  created() {
    // 初始化加载数据
    this.fetchTableData();
  },
  methods: {
     handleTitleClick(row) {
    if (row.url) {
      window.open(row.url, '_blank');
    } else {
      this.$message.warning('该文章没有关联的URL');
    }
  },
    // 获取表格数据
    async fetchTableData() {
      this.tableLoading = true;
      try {
        // 模拟接口请求，实际项目替换为真实接口
        const res = await this.$request.post("/system/carbonArticle/search", {
          currentPage: this.currentPage,
          pageSize: this.pageSize,
          ...this.searchForm,
        });
        if (res.code === 200) {
          this.tableData = res.data.list;
          this.totalCount = res.data.total;
        }
        
      } catch (error) {
        console.error("获取数据失败:", error);
        this.$message.error("网络错误，请重试");
      } finally {
        this.tableLoading = false;
      }
    },
    // 搜索
    handleSearch() {
      this.currentPage = 1;
      this.fetchTableData();
    },
    // 收藏操作
  handleCollect(row) {
  // 记录当前点击的按钮
  const button = document.activeElement;
  
  // 弹出提示弹窗
  this.$confirm(
    '请进入飞书文档，点击标题右侧按钮收藏！', 
    '提示', 
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'info'
    }
  ).then(() => {
    // 点击确定后跳转
    this.handleTitleClick(row);
  }).catch(() => {
    // 点击取消不做操作
  }).finally(() => {
    // 无论弹窗确定/取消，都移除按钮焦点
    if (button && button.blur) {
      setTimeout(() => {
        button.blur();
        document.body.focus();
      }, 100);
    }
  });
},
    // 分页大小改变
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.fetchTableData();
    },
    // 当前页改变
    handleCurrentChange(val) {
      this.currentPage = val;
      this.fetchTableData();
    },
  },
};
</script>

<style scoped>
.clickable-title {
  cursor: pointer; /* 手型光标 */
  transition: color 0.2s;
}

.clickable-title:hover {
  color: #66b1ff; /* hover时浅色 */
}
.knowledge-article-management {
  padding: 20px;
  background-color: #fff;
  min-height: calc(100vh - 40px);
}

.search-section {
  margin-bottom: 16px;
}

.pagination-container {
  margin-top: 20px;
  text-align: right;
}

.el-table {
  width: 100%;
}
</style>
